#define _CRT_SECURE_NO_WARNINGS
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseKGroup(struct ListNode* head, int k) {
    struct ListNode* arr[5000];
    struct ListNode* split = head;
    struct ListNode* check = head;
    int cnt = 0; int round = 0;
    while (check)
    {
        cnt++;
        check = check->next;
    }
    if (cnt < k)
        return head;
    while (cnt >= k)
    {
        for (int i = 0; i < k; i++)
        {
            arr[i + round * k] = split;
            split = split->next;
        }
        arr[0 + round * k]->next = arr[(k - 1) + round * k]->next;
        for (int j = 0; j < k - 1; j++)
        {
            arr[(j + 1) + round * k]->next = arr[j + round * k];
            if (round >= 1)
                arr[round * k - k]->next = arr[(j + 1) + round * k];
        }
        cnt -= k;
        round++;
    }
    head = arr[k - 1];
    return head;
}